//
//  TeacherBasicInformationController.m
//  BigToBig
//
//  Created by yi liu on 16/8/5.
//  Copyright © 2016年 cn.cloudtp. All rights reserved.
//
#import "TeacherBasicInformationController.h"
#import "TeacherBasicInformationHeaderView.h"
#import "BasicInfoTableViewCell.h"
#import "ProjectDetailViewController.h"
#import "TeacherBasicModel.h"
#import "UIImageView+WebCache.h"
@interface TeacherBasicInformationController ()
<
UITableViewDelegate,
UITableViewDataSource
>

@property (nonatomic , strong)UITableView *mainTableView;
@property (nonatomic, strong)TeacherBasicInformationHeaderView *teacherHeaderView;

@property (nonatomic , strong)NSArray *content;

@property (nonatomic , strong)NSString *info;

@property (nonatomic , assign)BOOL isSelected;
@property (nonatomic , assign)BOOL isFocusSelected;
@property (nonatomic , assign)NSInteger projectCount;
@property (nonatomic , strong)NSMutableArray *modelArr;


@end

@implementation TeacherBasicInformationController

- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    [self createData];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationController.navigationBar.barTintColor = COLOR_RGBA(77, 155, 255, 1);
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    titleLab.text = @"个人信息";
    titleLab.textColor = [UIColor whiteColor];
    [self setBackBar];
    titleLab.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLab;
    [self createView];
    
    
}
#pragma mark 创建数据
- (void)createData{
    
    self.modelArr = [NSMutableArray new];
    NSUserDefaults* userID = [NSUserDefaults standardUserDefaults];
    NSString *UID = [userID objectForKey:@"userID"];
    NSLog(@"%@",UID);
    NSDictionary *params = @{@"user_id":UID};
    NSString *urlStr = [NSString stringWithFormat:@"http://61.135.176.43:6080/bigtobig/notice_project/show_project?user_id=%@&offset=0&limit=10",UID];
    [HttpTool textHTMLPostWithURL:urlStr params:params success:^(id json) {
        NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSNumber *count = [dict objectForKey:@"count"];
        NSArray *noticeProjectsArr = [dict objectForKey:@"notice_projects"];
        self.projectCount = count.integerValue;
        for (NSDictionary *dic in noticeProjectsArr) {
            TeacherBasicModel *model = [TeacherBasicModel TeacherBasicModelWithDict:dic];
            [self.modelArr addObject:model];
        }
        NSLog(@"%d %@",__LINE__,noticeProjectsArr);
        [self.mainTableView reloadData];

    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];

    self.info = @"自我介绍是向别人展示你自己的一个重要手段, 自我介绍好不好, 甚至直接关系到你给别人的第一印象的好坏及以后交往的顺利与否. 同时, 也是认识自我的手段. 自我介绍是每一个职场中人都必然要经历的一件事情, 只不过.";
    self.content = @[@"最高学历",@"关注的项目",@"指导的创业者"];
}

#pragma mark 导航栏左右Button
- (void)setBackBar{
    
    BackButton *backBtn=[BackButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 44, 44);
    [backBtn addTarget:self action:@selector(backController:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backBtn.imageView setContentMode:UIViewContentModeLeft];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    [self.navigationItem setHidesBackButton:YES animated:YES];
    
    
}

- (void)backController:(UIButton *)button{
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 创建View
- (void)createView{
    self.isSelected = NO;
    self.isFocusSelected = NO;
    self.teacherHeaderView = [[TeacherBasicInformationHeaderView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 20)];
    self.teacherHeaderView.nameLabel.text = @"DADA";
    self.teacherHeaderView.jobLabel.text = @"云真UI";
    self.teacherHeaderView.personalInfoLabel.text = self.info;
    self.teacherHeaderView.backgroundColor = [UIColor whiteColor];
    self.teacherHeaderView.headImageView.image = [UIImage imageNamed:@"touxiang"];
    self.teacherHeaderView.groupLabel.text = @"创业导师";
    [self.teacherHeaderView layoutSubviews];
    
    // 主tableView
    self.mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStyleGrouped];
    self.mainTableView.tableHeaderView = self.teacherHeaderView;
    self.mainTableView.tableHeaderView.height = self.teacherHeaderView.personalInfoLabel.maxY + 10;
    [self.view addSubview:_mainTableView];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    [self.mainTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    [self.mainTableView registerClass:[BasicInfoTableViewCell class] forCellReuseIdentifier:NSStringFromClass([BasicInfoTableViewCell class])];
    self.mainTableView.tableFooterView = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
//    self.mainTableView.sectionHeaderHeight = 40;
//    self.mainTableView.sectionFooterHeight = 0;
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (self.isSelected) {
        if (self.isFocusSelected) {
            return 3 + self.projectCount + self.projectCount;
        }
        else{
            return 3 + self.projectCount;
        }
    }
    else if (self.isFocusSelected){
        return 3 + _projectCount;
    }
    else{
        return 3;
    }
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        if (self.isSelected == NO && self.isFocusSelected == NO) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
            cell.textLabel.text = self.content[indexPath.row];
            if(indexPath.row > 0){
                UIImageView *xlImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 15, 15)];
                xlImageView.image = [UIImage  imageNamed:@"xl-button"];
                xlImageView.center = CGPointMake(kScreenWidth - 30, cell.height / 2);
                [cell.contentView addSubview:xlImageView];
            }
            cell.tag = indexPath.row;
            NSLog(@"%ld",cell.tag);
            return cell;
        }
        else if(self.isFocusSelected == YES &&self.isSelected == NO){
            if (indexPath.row < 2 ||indexPath.row == 2 + self.projectCount) {
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
                if (indexPath.row < 2) {
                    cell.textLabel.text = self.content[indexPath.row];
                    
                }
                else{
                    cell.textLabel.text = self.content[indexPath.row - self.projectCount];
                }
                cell.tag = indexPath.row;
                return cell;
            }
            else{
                BasicInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([BasicInfoTableViewCell class]) forIndexPath:indexPath];
                TeacherBasicModel *model = self.modelArr[indexPath.row - 2];
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                [cell.headPic sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.bigtobig.com/%@",model.project_logo]] placeholderImage:[UIImage imageNamed:@"touxiang"]];
                cell.nameLabel.text = model.project_name;
                cell.describe.text = model.project_brief_introduction;
                return cell;
            }
        }
        else if(self.isFocusSelected == NO && self.isSelected == YES){
            if (indexPath.row < 3) {
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
                cell.textLabel.text = self.content[indexPath.row];
                cell.tag = indexPath.row;
                return cell;
            }
            else{
                BasicInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([BasicInfoTableViewCell class]) forIndexPath:indexPath];
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.headPic.image = [UIImage imageNamed:@"touxiang"];
                cell.nameLabel.text = @"创业生态园";
                cell.describe.text = @"纯天然无污染的";
                
                return cell;
            }
        }
        else{
            if (indexPath.row < 2 || indexPath.row == 2 + self.projectCount) {
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
                if (indexPath.row < 2) {
                    cell.textLabel.text = self.content[indexPath.row];
                    
                }
                else{
                    cell.textLabel.text = self.content[indexPath.row - self.projectCount];
                }
                cell.tag = indexPath.row;
                return cell;
            }
            else{
                BasicInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([BasicInfoTableViewCell class]) forIndexPath:indexPath];
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.headPic.image = [UIImage imageNamed:@"touxiang"];
                cell.nameLabel.text = @"创业生态园";
                cell.describe.text = @"纯天然无污染的";
                
                return cell;
            }
        }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        if (indexPath.row == 1) {
            if (self.isFocusSelected == NO) {
                self.isFocusSelected = YES;
                
            }
            else{
                self.isFocusSelected = NO;
            }
        }
        if (indexPath.row == 2|| indexPath.row == 2 + self.projectCount) {
            if (self.isSelected == NO && self.isFocusSelected == NO) {
                self.isSelected = YES;
                
            }
            else if(self.isFocusSelected == YES && indexPath.row == 2 + self.projectCount){
                if (self.isSelected == YES) {
                    self.isSelected = NO;
                }
                else{
                    self.isSelected = YES;
                }
            }
            else{
                if(self.isFocusSelected == YES && indexPath.row == 2){
                    
                }
                else{
                    self.isSelected = NO;
                    
                }
            }
        }
        if (indexPath.row > 1 && indexPath.row <= 1 + self.projectCount && self.isFocusSelected == YES) {
            ProjectDetailViewController *detailVC = [[ProjectDetailViewController alloc]init];
            TeacherBasicModel *model = self.modelArr[indexPath.row - 2];
            detailVC.project_id = model.project_id;
            [self.navigationController pushViewController:detailVC animated:YES];
        }
        
        
    
    
    [tableView reloadData];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
